package main

import (
	"encoding/json"
	"fmt"
	"io"
	"log"
	cmd "mvc/command"
	"mvc/model"
	"net/http"
)

/////////////////////////////////////////////////////////////////////////////////

func doWork(PackageName, FuncName, Params interface{}) string {

	var ret = ""
	var pkName = PackageName.(string) //获取模块名称
	var funName = FuncName.(string)   //获取调用模块方法名称
	//------开始判断调用对应模块功能
	if pkName == "model" {
		if funName == "getdata" {
			ret = model.Getdata(Params)
		} else if funName == "getdata1" {
			ret = model.Getdata1(Params)
		}
	}
	//----------------------------

	return ret
}

////////////////////////下面为服务部分////////////////////////////////////////////
func Resquest(w http.ResponseWriter, req *http.Request) {
	params := make(map[string]interface{})
	json.NewDecoder(req.Body).Decode(&params)
	fmt.Println(cmd.Nowtime(), "Method =", req.Method)
	fmt.Println("PackageName =", params["PackageName"], "FuncName =", params["FuncName"], "Params =", params["Params"])
	var retdata = doWork(params["PackageName"], params["FuncName"], params["Params"])
	w.Header().Set("Content-Type", "application/json")
	io.WriteString(w, retdata)
}
func main() {
	var Port = "9999"
	fmt.Println("MVC-Go Server Port:" + Port)
	http.HandleFunc("/", Resquest)
	err := http.ListenAndServe(":"+Port, nil)
	if err != nil {
		log.Fatal("ListenAndServe: ", err)
	}
}
